#include "user_extern.h"

static uint8_t run(void);
static void win_create(void);
static void win_destory(void);
static void update_screen(void);
static void display(void);
static uint8_t button_select(void *arg);
static uint8_t button_enter(void *arg);
static uint8_t button_exit(void *arg);
static uint8_t get_id(void);
static uint8_t get_father_id(void);

static uint8_t subidx = WIN_SUI_SETTING_WIFI;

static Windows window = {
    .id = WIN_UI_SETTING,
    .fa_id = WIN_UI_DESKTOP,
    .son_id = {WIN_SUI_SETTING_WIFI, WIN_SUI_SETTING_TIME,
               WIN_SUI_SETTING_LIGHT, WIN_SUI_SETTING_TONE,
               WIN_SUI_SETTING_WEATHER, WIN_SUI_SETTING_OTA,
               WIN_SUI_SETTING_STATUS},
    .name = "desktop",
    .win_status = WINDOWS_STATUS_INIT,
    .run = run,
    .update_screen = update_screen,
    .Dselect = button_select,
    .enter = button_enter,
    .exit = button_exit,
};

static void display(void)
{
    ui.draw_sysicon(44, 7, subidx);
    switch (subidx)
    {
    case WIN_SUI_SETTING_WIFI:
        oled.show_string(16, 48, T(const_setting_wifi), STR_SIZE_16, 1);
        break;
    case WIN_SUI_SETTING_TIME:
        oled.show_string(16, 48, T(const_setting_time), STR_SIZE_16, 1);
        break;
    case WIN_SUI_SETTING_LIGHT:
        oled.show_string(12, 48, T(const_setting_light), STR_SIZE_16, 1);
        break;
    case WIN_SUI_SETTING_TONE:
        oled.show_string(16, 48, T(const_setting_tone), STR_SIZE_16, 1);
        break;
    case WIN_SUI_SETTING_WEATHER:
        oled.show_string(4, 48, T(const_setting_weather), STR_SIZE_16, 1);
        break;
    case WIN_SUI_SETTING_OTA:
        oled.show_string(20, 48, T(const_setting_ota), STR_SIZE_16, 1);
        break;
    case WIN_SUI_SETTING_STATUS:
        oled.show_string(12, 48, T(const_setting_status), STR_SIZE_16, 1);
        break;
    }
}

static void update_screen(void)
{
    oled.refresh_all(1);
}

static void win_create(void)
{
    subidx = WIN_SUI_SETTING_WIFI;
}

static void win_destory(void)
{
    subidx = 0;
    window.win_status = WINDOWS_STATUS_INIT;
}

static uint8_t button_select(void *arg)
{
    subidx++;
    if (subidx > 6)
    {
        Dsystem.windows_cur_id = window.fa_id;
        window.win_status = WINDOWS_STATUS_DESTORY;
    }

    return 0;
}

static uint8_t button_enter(void *arg)
{
    Dsystem.windows_cur_id = WIN_UI_OTHERMO;
    Dsystem.windows_sub_id = window.son_id[subidx - WIN_SUI_SETTING_WIFI];
    //window.win_status = WINDOWS_STATUS_DESTORY;
    return 0;
}

static uint8_t button_exit(void *arg)
{
    Dsystem.windows_cur_id = get_father_id();
    window.win_status = WINDOWS_STATUS_DESTORY;
    return 0;
}

/* **************************************************************************************** */
static uint8_t run(void)
{

    switch (window.win_status)
    {
    case WINDOWS_STATUS_NONE:
        return 0;
        break;
    case WINDOWS_STATUS_INIT:
        win_create();
        display();
        window.win_status = WINDOWS_STATUS_DIS_ONLY;
        break;
    case WINDOWS_STATUS_DIS_ONLY:
        display();
        break;
    case WINDOWS_STATUS_DESTORY:
        win_destory();
        break;
    }
    return window.win_status;
}

static uint8_t get_id(void)
{
    return window.id;
}

static uint8_t get_father_id(void)
{
    return window.fa_id;
}

void win02_setting_init(void)
{
    windows[get_id()] = window;
}
